package com.neusoft.neuiotms.dcom.converter;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.InspectionTaskProjectDTO;
import com.neusoft.neuiotms.dcom.dto.InspectionTaskProjectItemValueDTO;
import com.neusoft.neuiotms.dcom.model.InspectionTaskProject;
import com.neusoft.neuiotms.dcom.model.InspectionTaskProjectItemValue;
import com.neusoft.neuiotms.dcom.model.enums.InspectionItemValueStatus;
import com.neusoft.neuiotms.dcom.repository.InspectionTaskProjectRepository;

@Component
public class InspectionTaskProjectConverter extends AbstractConverter<InspectionTaskProject, InspectionTaskProjectDTO> {

    @Autowired
    private InspectionTaskProjectItemValueConverter itemValueConverter;

    @Autowired
    private InspectionTaskProjectRepository projectRepository;

    @Override
    public InspectionTaskProject toModel(final InspectionTaskProjectDTO dto) {
        InspectionTaskProject model;
        if (dto.isNew()) {
            model = new InspectionTaskProject();
        } else {
            model = this.projectRepository.getOne(dto.getId());
        }

        model.setConcreteCode(dto.getConcreteCode());
        model.setConcreteName(dto.getConcreteName());
        model.setLocationCode(dto.getLocationCode());
        model.setLocationName(dto.getLocationName());
        if ((dto.getItemValue() != null) && !dto.getItemValue().isEmpty()) {
            final List<InspectionTaskProjectItemValue> itemValues =
                    this.itemValueConverter.toListModel(dto.getItemValue());
            Integer normal = 0;
            Integer alarm = 0;
            Integer earlyWarning = 0;
            Integer uninspected = 0;
            for (final InspectionTaskProjectItemValue item : itemValues) {
                if (item.getStatus() == InspectionItemValueStatus.alarm) {
                    alarm++;
                } else if (item.getStatus() == InspectionItemValueStatus.earlyWarning) {
                    earlyWarning++;
                } else if (item.getStatus() == InspectionItemValueStatus.normal) {
                    normal++;
                } else if (item.getStatus() == InspectionItemValueStatus.uninspected) {
                    uninspected++;
                } else {

                }
            }
            model.setNormal(normal);
            model.setAlarm(alarm);
            model.setEarlyWarning(earlyWarning);
            model.setUninspected(uninspected);
            model.setItemValue(itemValues);
        }
        return model;
    }

    @Override
    public InspectionTaskProjectDTO toDTO(final InspectionTaskProject model, final boolean forListView) {
        final InspectionTaskProjectDTO dto = new InspectionTaskProjectDTO();
        dto.setConcreteCode(model.getConcreteCode());
        dto.setConcreteName(model.getConcreteName());
        dto.setId(model.getId());
        dto.setLocationCode(model.getLocationCode());
        dto.setLocationName(model.getLocationName());
        dto.setAlarm(model.getAlarm());
        dto.setEarlyWarning(model.getEarlyWarning());
        dto.setUninspected(model.getUninspected());
        dto.setNormal(model.getNormal());
        if (!forListView) {
            if ((model.getItemValue() != null) && !model.getItemValue().isEmpty()) {
                final List<InspectionTaskProjectItemValueDTO> itemValues = model.getItemValue().stream().map(m -> {
                    return this.itemValueConverter.toDTO(m, forListView);
                }).sorted(Comparator.comparing(InspectionTaskProjectItemValueDTO::getSort))
                        .collect(Collectors.toList());
                dto.setItemValue(itemValues);
            }
        }

        return dto;
    }
}
